﻿importClass('rapi.rbase.RXMLDocument');
function RXmlParser(){
    this.ObjectToXml=function(obj,xmlstr,bEncryptValues){
        var docXml=new RXMLDocument();
        Xml=docXml.get();
        var node=this.ObjectToNode(obj,xmlstr,bEncryptValues);
        return Xml.appendChild(node);
    }
    
    this.ObjectToNode=function(obj, xmlstr, bEncryptValues){
        var docXml=new RXMLDocument();
        Xml=docXml.get();
        var node;
        if(!isNaN(xmlstr)){
            node=Xml.createElement("RArray");
        }else{
            node=Xml.createElement(xmlstr);
        }
        for(var objChild in obj){
            if(typeof(obj[objChild])=="function"){
                continue;
            }
            if(typeof(obj[objChild])=="object"){
                var temXml=this.ObjectToNode(obj[objChild],objChild,bEncryptValues);
                node.appendChild(temXml);
                continue;
            }
            var childNode;
            if(!isNaN(objChild)){
                childNode=Xml.createElement("RArray");
            }else{
                childNode=Xml.createElement(objChild);
            }            
            childNode.text=obj[objChild];
            node.appendChild(childNode);
        }
        return node;
    }
    
    this.XMLtoObject=function(docXml){
        var obj=new Object();               
        if(docXml){
            for(var index=0;index<docXml.childNodes.length;index++){
                if(docXml.childNodes[index].nodeType==1||docXml.childNodes[index].nodeType==9){
                    this.ParseXML(docXml.childNodes[index],obj);
                }
            } 
        }             
        return obj;
    }
    
    this.ParseXML=function(docXml,objTarget){
        if(docXml.attributes){
            for(var i=0;i<docXml.attributes.length;i++){
                objTarget[docXml.attributes[i].nodeName]=docXml.attributes[i].nodeValue;
            }
        }                 
        var childs=docXml.childNodes;
        if(childs.length==0){
            objTarget.nodeValue="";
        }
        for(i=0;i<childs.length;i++){
            if(childs[i].childNodes.length==0&&childs[i].nodeType==3){
                objTarget.nodeValue=(childs[i].text||childs[i].textContent);
            }else{
                if(typeof(objTarget[childs[i].nodeName])=='undefined'){                    
                    objTarget[childs[i].nodeName]=new Object();                    
                    objTarget[childs[i].nodeName].Rtype="obj";
                    this.ParseXML(childs[i],objTarget[childs[i].nodeName]);
                }else if(objTarget[childs[i].nodeName].Rtype=="obj"){                    
                    var objTemp=objTarget[childs[i].nodeName];
                    objTarget[childs[i].nodeName]=new Array();
                    objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length]=objTemp;
                    objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length]=new Object;
                    this.ParseXML(childs[i],objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length-1]);
                }else{                
                    objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length]=new Object;
                    this.ParseXML(childs[i],objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length-1]);                   
                }
            }            
        }
        return objTarget;
    }
    
    this.XMLtoArray=function(docXml){
        var obj=new Object();        
        if(docXml){
            for(var index=0;index<docXml.childNodes.length;index++){
                if(docXml.childNodes[index].nodeType==1||docXml.childNodes[index].nodeType==9){
                    this.ParseXMLAry(docXml.childNodes[index],obj);
                }
            }
        }                
        return obj;
    }
    
    this.ParseXMLAry=function(docXml, objTarget){
        for(var i=0;i<docXml.attributes.length;i++){
            objTarget[docXml.attributes[i].nodeName]=docXml.attributes[i].nodeValue;
        }         
        var childs=docXml.childNodes;
        if(childs.length==0){
            objTarget.nodeValue="";
        }
        for(i=0;i<childs.length;i++){
            if(childs[i].childNodes.length==0&&childs[i].nodeType==3){
                objTarget.nodeValue=(childs[i].text||childs[i].textContent);
            }else{               
                if(typeof(objTarget[childs[i].nodeName])=='undefined'){
                    objTarget[childs[i].nodeName]=new Array();                        
                    objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length]=new Object;
                    this.ParseXMLAry(childs[i],objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length-1]);
                }else{
                    objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length]=new Object;
                this.ParseXMLAry(childs[i],objTarget[childs[i].nodeName][objTarget[childs[i].nodeName].length-1]);
                }
            }            
        }
        return objTarget;
    }

    
//    this.Encrypt=function(theText){
//        var reText=new String();

//    }
//    
//    this.Decrypt=function(theText){
//    
//    } 
}

